package pt.mdc.android.mysensors;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class SensorDetails extends Activity implements SensorEventListener {

	private SensorManager mySensorManager;
	private Sensor sensor;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		

		super.onCreate(savedInstanceState);
		setContentView(R.layout.sensordetails);
		TextView tv = (TextView) findViewById(R.id.TextView01);

		// get sensor manager instance
		mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

		// get sensor id
		Bundle bundle = this.getIntent().getExtras();
		Integer sensor_id = bundle.getInt("sensor_id");

		sensor = mySensorManager.getSensorList(Sensor.TYPE_ALL).get(sensor_id);

		String text = new String();
		text += "Details of " + sensor.getName() + "\n\n";

		int type = sensor.getType();
		text += " Type: ";
		switch (type) {
		case 1:
			text += "Accelerometer";
			break;
		case 2:
			text += "Magnetic Field";
			break;
		case 3:
			text += "Orientation";
			break;
		case 4:
			text += "Gyroscope";
			break;
		case 5:
			text += "Light";
			break;
		case 6:
			text += "Pressure";
			break;
		case 7:
			text += "Temperature";
			break;
		case 8:
			text += "Proximity";
			break;
		default:
			text += "Unknown";
			break;
		}
		text += "\n";

		text += " Power: " + String.valueOf(sensor.getPower()) + "mA\n";
		text += " Res: " + String.valueOf(sensor.getResolution()) + "\n";
		text += " MaxRange: " + String.valueOf(sensor.getMaximumRange()) + "\n";
		text += " Vendor: " + sensor.getVendor() + "\n";
		text += " Version: " + String.valueOf(sensor.getVersion()) + "\n";

		tv.setText(text);
		
		// register listener
		mySensorManager.registerListener(this, sensor, mySensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		
		TextView tv = (TextView) findViewById(R.id.TextView02);

		String text = new String(" Accuracy: ");
		text += accuracy + "\n";

		tv.setText(text);
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		TextView tv = (TextView) findViewById(R.id.TextView03);
		
		String text = new String(" Event:\n");
		
//		text += " TimeStamp: " + event.timestamp + "\n";
		
//		text += " Data:\n";
		
		int type = sensor.getType();
		switch (type) {
		case 1:
			text += "  Acceleration - Gravity (m/s^2):\n";
			text += "   x: " + event.values[0];
			text += "   y: " + event.values[1];
			text += "   z: " + event.values[2];
			break;
		case 2:
			text += "  Magnetic Field (muT):\n";
			text += "   x: " + event.values[0];
			text += "   y: " + event.values[1];
			text += "   z: " + event.values[2];
			break;
		case 3:
			text += "  Angles (degrees):\n";
			text += "   Azimuth: " + event.values[0];
			text += "   Pitch: " + event.values[1];
			text += "   Roll: " + event.values[2];
			break;
		case 4:
			text += "  Gyroscope:";
			break;
		case 5:
			text += "  Ambient Light (lux): ";
			text += event.values[0];
			break;
		case 6:
			text += "  Pressure:";
			break;
		case 7:
			text += "  Temperature:";
			break;
		case 8:
			text += "  Proximity (cm): ";
			text += event.values[0];
			break;
		default:
			text += "  Unknown";
			break;
		}
		text += "\n";
		
		
		tv.setText(text);
		
		
		
	}

}